#include "StdAfx.hpp"
#include "MathVariableTypes.hpp"

using namespace common;


////////////////////////////////////////////////////////////////////////////////
// class Vec2Variable

void Vec2Variable::LoadText(IParamObject &obj, common::Tokenizer &tok)
{
	VEC2 val;

	tok.AssertSymbol('{');
	tok.Next();
	val.x = tok.MustGetFloat();
	tok.Next();
	tok.AssertSymbol(',');
	tok.Next();
	val.y = tok.MustGetFloat();
	tok.Next();
	tok.AssertSymbol('}');
	tok.Next();

	SetValue(obj, val);
}

void Vec2Variable::SaveText(const IParamObject &obj, TokenWriter &tw)
{
	VEC2 val;
	GetValue(obj, val);
	
	tw.WriteSymbol('{');
	tw.WriteFloat(val.x);
	tw.WriteSymbol(',');
	tw.WriteFloat(val.y);
	tw.WriteSymbol('}');
}


////////////////////////////////////////////////////////////////////////////////
// class Vec3Variable

void Vec3Variable::LoadText(IParamObject &obj, common::Tokenizer &tok)
{
	VEC3 val;

	tok.AssertSymbol('{');
	tok.Next();
	val.x = tok.MustGetFloat();
	tok.Next();
	tok.AssertSymbol(',');
	tok.Next();
	val.y = tok.MustGetFloat();
	tok.Next();
	tok.AssertSymbol(',');
	tok.Next();
	val.z = tok.MustGetFloat();
	tok.Next();
	tok.AssertSymbol('}');
	tok.Next();

	SetValue(obj, val);
}

void Vec3Variable::SaveText(const IParamObject &obj, TokenWriter &tw)
{
	VEC3 val;
	GetValue(obj, val);
	
	tw.WriteSymbol('{');
	tw.WriteFloat(val.x);
	tw.WriteSymbol(',');
	tw.WriteFloat(val.y);
	tw.WriteSymbol(',');
	tw.WriteFloat(val.z);
	tw.WriteSymbol('}');
}


////////////////////////////////////////////////////////////////////////////////
// class Vec4Variable

void Vec4Variable::LoadText(IParamObject &obj, common::Tokenizer &tok)
{
	VEC4 val;

	tok.AssertSymbol('{');
	tok.Next();
	val.x = tok.MustGetFloat();
	tok.Next();
	tok.AssertSymbol(',');
	tok.Next();
	val.y = tok.MustGetFloat();
	tok.Next();
	tok.AssertSymbol(',');
	tok.Next();
	val.z = tok.MustGetFloat();
	tok.Next();
	tok.AssertSymbol(',');
	tok.Next();
	val.w = tok.MustGetFloat();
	tok.Next();
	tok.AssertSymbol('}');
	tok.Next();

	SetValue(obj, val);
}

void Vec4Variable::SaveText(const IParamObject &obj, TokenWriter &tw)
{
	VEC4 val;
	GetValue(obj, val);
	
	tw.WriteSymbol('{');
	tw.WriteFloat(val.x);
	tw.WriteSymbol(',');
	tw.WriteFloat(val.y);
	tw.WriteSymbol(',');
	tw.WriteFloat(val.z);
	tw.WriteSymbol(',');
	tw.WriteFloat(val.w);
	tw.WriteSymbol('}');
}


////////////////////////////////////////////////////////////////////////////////
// class ColorVariable

void ColorVariable::LoadText(IParamObject &obj, common::Tokenizer &tok)
{
	COLOR val;
	val.ARGB = tok.MustGetUint4();
	tok.Next();

	SetValue(obj, val);
}

void ColorVariable::SaveText(const IParamObject &obj, TokenWriter &tw)
{
	COLOR val;
	GetValue(obj, val);
	
	string s;
	if (UseAlpha)
		UintToStr2(&s, val.ARGB, 8, 16);
	else
		UintToStr2(&s, val.ARGB & 0xffffff, 6, 16);
	
	tw.WriteNumberString("0x" + s);
}


////////////////////////////////////////////////////////////////////////////////
// class ColorVariable

void ColorfVariable::LoadText(IParamObject &obj, common::Tokenizer &tok)
{
	COLORF val;

	tok.AssertSymbol('{');
	tok.Next();
	val.R = tok.MustGetFloat();
	tok.Next();
	tok.AssertSymbol(',');
	tok.Next();
	val.G = tok.MustGetFloat();
	tok.Next();
	tok.AssertSymbol(',');
	tok.Next();
	val.B = tok.MustGetFloat();
	tok.Next();
	tok.AssertSymbol(',');
	tok.Next();
	val.A = tok.MustGetFloat();
	tok.Next();
	tok.AssertSymbol('}');
	tok.Next();

	SetValue(obj, val);
}

void ColorfVariable::SaveText(const IParamObject &obj, TokenWriter &tw)
{
	COLORF val;
	GetValue(obj, val);
	
	tw.WriteSymbol('{');
	tw.WriteFloat(val.R);
	tw.WriteSymbol(',');
	tw.WriteFloat(val.G);
	tw.WriteSymbol(',');
	tw.WriteFloat(val.B);
	tw.WriteSymbol(',');
	tw.WriteFloat(val.A);
	tw.WriteSymbol('}');
}


////////////////////////////////////////////////////////////////////////////////
// class QuaternionVariable

void QuaternionVariable::LoadText(IParamObject &obj, common::Tokenizer &tok)
{
	QUATERNION val;

	tok.AssertSymbol('{');
	tok.Next();
	val.x = tok.MustGetFloat();
	tok.Next();
	tok.AssertSymbol(',');
	tok.Next();
	val.y = tok.MustGetFloat();
	tok.Next();
	tok.AssertSymbol(',');
	tok.Next();
	val.z = tok.MustGetFloat();
	tok.Next();
	tok.AssertSymbol(',');
	tok.Next();
	val.w = tok.MustGetFloat();
	tok.Next();
	tok.AssertSymbol('}');
	tok.Next();

	SetValue(obj, val);
}

void QuaternionVariable::SaveText(const IParamObject &obj, TokenWriter &tw)
{
	QUATERNION val;
	GetValue(obj, val);
	
	tw.WriteSymbol('{');
	tw.WriteFloat(val.x);
	tw.WriteSymbol(',');
	tw.WriteFloat(val.x);
	tw.WriteSymbol(',');
	tw.WriteFloat(val.z);
	tw.WriteSymbol(',');
	tw.WriteFloat(val.w);
	tw.WriteSymbol('}');
}

////////////////////////////////////////////////////////////////////////////////
// class PointVariable

void PointVariable::LoadText(IParamObject &obj, common::Tokenizer &tok)
{
	POINT_ val;

	tok.AssertSymbol('{');
	tok.Next();
	val.x = tok.MustGetInt4();
	tok.Next();
	tok.AssertSymbol(',');
	tok.Next();
	val.y = tok.MustGetInt4();
	tok.Next();
	tok.AssertSymbol('}');
	tok.Next();

	SetValue(obj, val);
}

void PointVariable::SaveText(const IParamObject &obj, TokenWriter &tw)
{
	POINT_ val;
	GetValue(obj, val);
	
	tw.WriteSymbol('{');
	tw.WriteInt4(val.x);
	tw.WriteSymbol(',');
	tw.WriteInt4(val.y);
	tw.WriteSymbol('}');
}


////////////////////////////////////////////////////////////////////////////////
// class RectVariable

void RectVariable::LoadText(IParamObject &obj, common::Tokenizer &tok)
{
	RECTI val;

	tok.AssertSymbol('{');
	tok.Next();
	val.Min.x = tok.MustGetInt4();
	tok.Next();
	tok.AssertSymbol(',');
	tok.Next();
	val.Min.y = tok.MustGetInt4();
	tok.Next();
	tok.AssertSymbol(';');
	tok.Next();
	val.Max.x = tok.MustGetInt4();
	tok.Next();
	tok.AssertSymbol(',');
	tok.Next();
	val.Max.y = tok.MustGetInt4();
	tok.Next();
	tok.AssertSymbol('}');
	tok.Next();

	SetValue(obj, val);
}

void RectVariable::SaveText(const IParamObject &obj, TokenWriter &tw)
{
	RECTI val;
	GetValue(obj, val);
	
	tw.WriteSymbol('{');
	tw.WriteInt4(val.Min.x);
	tw.WriteSymbol(',');
	tw.WriteInt4(val.Min.y);
	tw.WriteSymbol(';');
	tw.WriteInt4(val.Max.x);
	tw.WriteSymbol(',');
	tw.WriteInt4(val.Max.y);
	tw.WriteSymbol('}');
}


////////////////////////////////////////////////////////////////////////////////
// class RectfVariable

void RectfVariable::LoadText(IParamObject &obj, common::Tokenizer &tok)
{
	RECTF val;

	tok.AssertSymbol('{');
	tok.Next();
	val.Min.x = tok.MustGetFloat();
	tok.Next();
	tok.AssertSymbol(',');
	tok.Next();
	val.Min.y = tok.MustGetFloat();
	tok.Next();
	tok.AssertSymbol(';');
	tok.Next();
	val.Max.x = tok.MustGetFloat();
	tok.Next();
	tok.AssertSymbol(',');
	tok.Next();
	val.Max.y = tok.MustGetFloat();
	tok.Next();
	tok.AssertSymbol('}');
	tok.Next();

	SetValue(obj, val);
}

void RectfVariable::SaveText(const IParamObject &obj, TokenWriter &tw)
{
	RECTF val;
	GetValue(obj, val);
	
	tw.WriteSymbol('{');
	tw.WriteFloat(val.Min.x);
	tw.WriteSymbol(',');
	tw.WriteFloat(val.Min.y);
	tw.WriteSymbol(';');
	tw.WriteFloat(val.Max.x);
	tw.WriteSymbol(',');
	tw.WriteFloat(val.Max.y);
	tw.WriteSymbol('}');
}


////////////////////////////////////////////////////////////////////////////////
// class BoxVariable

void BoxVariable::LoadText(IParamObject &obj, common::Tokenizer &tok)
{
	BOX val;

	tok.AssertSymbol('{');
	tok.Next();
	val.Min.x = tok.MustGetFloat();
	tok.Next();
	tok.AssertSymbol(',');
	tok.Next();
	val.Min.y = tok.MustGetFloat();
	tok.Next();
	tok.AssertSymbol(',');
	tok.Next();
	val.Min.z = tok.MustGetFloat();
	tok.Next();
	tok.AssertSymbol(';');
	tok.Next();
	val.Max.x = tok.MustGetFloat();
	tok.Next();
	tok.AssertSymbol(',');
	tok.Next();
	val.Max.y = tok.MustGetFloat();
	tok.Next();
	tok.AssertSymbol(',');
	tok.Next();
	val.Max.z = tok.MustGetFloat();
	tok.Next();
	tok.AssertSymbol('}');
	tok.Next();

	SetValue(obj, val);
}

void BoxVariable::SaveText(const IParamObject &obj, TokenWriter &tw)
{
	BOX val;
	GetValue(obj, val);
	
	tw.WriteSymbol('{');
	tw.WriteFloat(val.Min.x);
	tw.WriteSymbol(',');
	tw.WriteFloat(val.Min.y);
	tw.WriteSymbol(',');
	tw.WriteFloat(val.Min.z);
	tw.WriteSymbol(';');
	tw.WriteFloat(val.Max.x);
	tw.WriteSymbol(',');
	tw.WriteFloat(val.Max.y);
	tw.WriteSymbol(',');
	tw.WriteFloat(val.Max.z);
	tw.WriteSymbol('}');
}



////////////////////////////////////////////////////////////////////////////////
// class MatrixVariable

void MatrixVariable::LoadText(IParamObject &obj, common::Tokenizer &tok)
{
	MATRIX val;

	tok.AssertSymbol('{'); tok.Next();
	val._11 = tok.MustGetFloat(); tok.Next(); tok.AssertSymbol(','); tok.Next();
	val._12 = tok.MustGetFloat(); tok.Next(); tok.AssertSymbol(','); tok.Next();
	val._13 = tok.MustGetFloat(); tok.Next(); tok.AssertSymbol(','); tok.Next();
	val._14 = tok.MustGetFloat(); tok.Next(); tok.AssertSymbol(';'); tok.Next();
	val._21 = tok.MustGetFloat(); tok.Next(); tok.AssertSymbol(','); tok.Next();
	val._22 = tok.MustGetFloat(); tok.Next(); tok.AssertSymbol(','); tok.Next();
	val._23 = tok.MustGetFloat(); tok.Next(); tok.AssertSymbol(','); tok.Next();
	val._24 = tok.MustGetFloat(); tok.Next(); tok.AssertSymbol(';'); tok.Next();
	val._31 = tok.MustGetFloat(); tok.Next(); tok.AssertSymbol(','); tok.Next();
	val._32 = tok.MustGetFloat(); tok.Next(); tok.AssertSymbol(','); tok.Next();
	val._33 = tok.MustGetFloat(); tok.Next(); tok.AssertSymbol(','); tok.Next();
	val._34 = tok.MustGetFloat(); tok.Next(); tok.AssertSymbol(';'); tok.Next();
	val._41 = tok.MustGetFloat(); tok.Next(); tok.AssertSymbol(','); tok.Next();
	val._42 = tok.MustGetFloat(); tok.Next(); tok.AssertSymbol(','); tok.Next();
	val._43 = tok.MustGetFloat(); tok.Next(); tok.AssertSymbol(','); tok.Next();
	val._44 = tok.MustGetFloat(); tok.Next(); tok.AssertSymbol('}'); tok.Next();

	SetValue(obj, val);
}

void MatrixVariable::SaveText(const IParamObject &obj, TokenWriter &tw)
{
	MATRIX val;
	GetValue(obj, val);
	
	tw.WriteSymbol('{');
	tw.WriteFloat(val._11); tw.WriteSymbol(',');
	tw.WriteFloat(val._12); tw.WriteSymbol(',');
	tw.WriteFloat(val._13); tw.WriteSymbol(',');
	tw.WriteFloat(val._14); tw.WriteSymbol(';');
	tw.WriteFloat(val._21); tw.WriteSymbol(',');
	tw.WriteFloat(val._22); tw.WriteSymbol(',');
	tw.WriteFloat(val._23); tw.WriteSymbol(',');
	tw.WriteFloat(val._24); tw.WriteSymbol(';');
	tw.WriteFloat(val._31); tw.WriteSymbol(',');
	tw.WriteFloat(val._32); tw.WriteSymbol(',');
	tw.WriteFloat(val._33); tw.WriteSymbol(',');
	tw.WriteFloat(val._34); tw.WriteSymbol(';');
	tw.WriteFloat(val._41); tw.WriteSymbol(',');
	tw.WriteFloat(val._42); tw.WriteSymbol(',');
	tw.WriteFloat(val._43); tw.WriteSymbol(',');
	tw.WriteFloat(val._44); tw.WriteSymbol('}');
}

